
// ***********************************************************************
// AUTO GENERATED CLASS - Do Not Modify this file, if needed modify .tt
// ***********************************************************************
// Assembly         : InnovaApp_DomainLayer
// Author           : LJO+01
// Created          : 17/11/2013 20:29:50
// ***********************************************************************
// <copyright file="N.A." company="Copyright © Innovacall 2011">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary>
// Domain layer  
// DomainClass
// </summary>
// ***********************************************************************

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;


namespace InnovaApp.Layers.DomainLayer
{
    /// <summary>
    /// Class Contact_ContactAddress
    /// </summary>
    [Serializable]
    public partial class Contact_ContactAddress : _DomainLayer_Base  
    {
        #region Primitive Properties
    	
        /// <summary>
        /// Gets or sets Id.
        /// </summary>
        /// <value>The Id.</value>
        public virtual int Id
        {
            get;
            set;
        }
    	
        /// <summary>
        /// Gets or sets FK_ContactAddress.
        /// </summary>
        /// <value>The FK_ContactAddress.</value>
        /// <remarks>
        /// Foregn key of Contact_ContactAddress
        /// 
        /// </remarks>
        public virtual int FK_ContactAddress
        {
            get { return _fK_ContactAddress; }
            set
            {
                if (_fK_ContactAddress != value)
                {
                    if (ContactAddress != null && ContactAddress.Id != value)
                    {
                        ContactAddress = null;
                    }
                    _fK_ContactAddress = value;
                }
            }
        }
        private int _fK_ContactAddress;
    	
        /// <summary>
        /// Gets or sets FK_Contact.
        /// </summary>
        /// <value>The FK_Contact.</value>
        /// <remarks>
        /// Foregn key of Contact_ContactAddress
        /// 
        /// </remarks>
        public virtual int FK_Contact
        {
            get { return _fK_Contact; }
            set
            {
                if (_fK_Contact != value)
                {
                    if (Contact != null && Contact.Id != value)
                    {
                        Contact = null;
                    }
                    _fK_Contact = value;
                }
            }
        }
        private int _fK_Contact;
    	
        /// <summary>
        /// Gets or sets AddressLabel.
        /// </summary>
        /// <value>The AddressLabel.</value>
        public virtual string AddressLabel
        {
            get;
            set;
        }
    	
        /// <summary>
        /// Gets or sets SortIndex.
        /// </summary>
        /// <value>The SortIndex.</value>
        public virtual int SortIndex
        {
            get;
            set;
        }
    	
        /// <summary>
        /// Gets or sets rowversion.
        /// </summary>
        /// <value>The rowversion.</value>
        public virtual byte[] rowversion
        {
            get;
            set;
        }

        #endregion

        #region Navigation Properties
        public virtual Contact Contact
        {
            get { return _contact; }
            set
            {
                if (!ReferenceEquals(_contact, value))
                {
                    var previousValue = _contact;
                    _contact = value;
                    FixupContact(previousValue);
                }
            }
        }
        private Contact _contact;
        public virtual ContactAddress ContactAddress
        {
            get { return _contactAddress; }
            set
            {
                if (!ReferenceEquals(_contactAddress, value))
                {
                    var previousValue = _contactAddress;
                    _contactAddress = value;
                    FixupContactAddress(previousValue);
                }
            }
        }
        private ContactAddress _contactAddress;
    
        /// <summary>
        /// Link Contact_ContactAddress to all Contact_ContactAddress_AddressType
        /// </summary>
        public virtual ICollection<Contact_ContactAddress_AddressType> Contact_ContactAddress_AddressType
        {
            get
            {
                if (_contact_ContactAddress_AddressType == null)
                {
                    var newCollection = new FixupCollection<Contact_ContactAddress_AddressType>();
                    newCollection.CollectionChanged += FixupContact_ContactAddress_AddressType;
                    _contact_ContactAddress_AddressType = newCollection;
                }
                return _contact_ContactAddress_AddressType;
            }
            set
            {
                if (!ReferenceEquals(_contact_ContactAddress_AddressType, value))
                {
                    var previousValue = _contact_ContactAddress_AddressType as FixupCollection<Contact_ContactAddress_AddressType>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupContact_ContactAddress_AddressType;
                    }
                    _contact_ContactAddress_AddressType = value;
                    var newValue = value as FixupCollection<Contact_ContactAddress_AddressType>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupContact_ContactAddress_AddressType;
                    }
                }
            }
        }
        private ICollection<Contact_ContactAddress_AddressType> _contact_ContactAddress_AddressType;

        #endregion

        #region Association Fixup
    
        private void FixupContact(Contact previousValue)
        {
            if (previousValue != null && previousValue.Contact_ContactAddress.Contains(this))
            {
                previousValue.Contact_ContactAddress.Remove(this);
            }
    
            if (Contact != null)
            {
                if (!Contact.Contact_ContactAddress.Contains(this))
                {
                    Contact.Contact_ContactAddress.Add(this);
                }
                if (FK_Contact != Contact.Id)
                {
                    FK_Contact = Contact.Id;
                }
            }
        }
    
        private void FixupContactAddress(ContactAddress previousValue)
        {
            if (previousValue != null && previousValue.Contact_ContactAddress.Contains(this))
            {
                previousValue.Contact_ContactAddress.Remove(this);
            }
    
            if (ContactAddress != null)
            {
                if (!ContactAddress.Contact_ContactAddress.Contains(this))
                {
                    ContactAddress.Contact_ContactAddress.Add(this);
                }
                if (FK_ContactAddress != ContactAddress.Id)
                {
                    FK_ContactAddress = ContactAddress.Id;
                }
            }
        }
    
        private void FixupContact_ContactAddress_AddressType(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Contact_ContactAddress_AddressType item in e.NewItems)
                {
                    item.Contact_ContactAddress = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (Contact_ContactAddress_AddressType item in e.OldItems)
                {
                    if (ReferenceEquals(item.Contact_ContactAddress, this))
                    {
                        item.Contact_ContactAddress = null;
                    }
                }
            }
        }

        #endregion

    }
}
